package com.jk11a.shangdaast.notice;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.jk11a.shangdaast.R;
import com.jk11a.shangdaast.dao.NoticeDao;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.text.Layout;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;

public class WeeklyNotice extends Activity {
	private Thread weeklyThread;
	private static final int MSG_SUCCESS = 0; // 获取成功
	private static final int MSG_FAILURE = 1; // 获取失败
	private SharedPreferences sp = null;
	private String FILE = "saveUserNamePwd";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notice_weekly);
		sp = this.getSharedPreferences(FILE, Context.MODE_PRIVATE);
		if (weeklyThread == null) {
			weeklyThread = new Thread(runnable);
			weeklyThread.start();
		} else {
			Toast.makeText(getApplication(), "weeklyThread thread 已启动",
					Toast.LENGTH_LONG).show();
		}
	}

	Runnable runnable = new Runnable() {
		// 重写run()方法，此方法在新的线程中运行
		@Override
		public void run() {
			// TODO Auto-generated method stub
			NoticeDao noticeDao = new NoticeDao();
			List<NoticeBean> notices = noticeDao.getweeklynotice(sp.getInt(
					"id", 0));
			// 获取 成功，向UI线程发送MSG_SUCCESS标识和books对象
			if (notices.size() != 0) {
				mHandler.obtainMessage(MSG_SUCCESS, notices).sendToTarget();
			} else {
				// 获取 失败，向UI线程发送MSG_FAILURE标识
				mHandler.obtainMessage(MSG_FAILURE).sendToTarget();
				return;
			}
		}
	};
	private Handler mHandler = new Handler() {
		// 重写handleMessage()方法，此方法在UI线程运行
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_SUCCESS:
				List<NoticeBean> notices = (List<NoticeBean>) msg.obj;
				popnoticelist(notices);
				break;

			case MSG_FAILURE:
				Toast.makeText(getApplication(), "获取周通知失败", Toast.LENGTH_LONG)
						.show();
				break;
			}
		}
	};

	private void popnoticelist(final List<NoticeBean> notices) {
		int j = 1;
		int i = 0;
		for (i = 0; i < notices.size(); i++) {
			RelativeLayout rLayout = new RelativeLayout(this);
			if (i == 0) {
				rLayout.setBackgroundResource(R.drawable.preference_first_item);
				rLayout.setTop(14);
			} else {
				rLayout.setBackgroundResource(R.drawable.preference_item);
			}
			rLayout.setClickable(true);
			rLayout.setLayoutParams(new RelativeLayout.LayoutParams(
					LayoutParams.FILL_PARENT, 80));
			rLayout.setGravity(Gravity.CENTER);
			TextView tv = new TextView(this);
			tv.setText(notices.get(i).getTitle() + "			"
					+ notices.get(i).getPublishtime() + "     发布人id："
					+ notices.get(i).getWriterid());
			tv.setLayoutParams(new RelativeLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			tv.setTextColor(Color.rgb(0, 0, 0));
			rLayout.addView(tv);
			setonclick(this, rLayout, notices, i);   //设置onclick 事件
			((LinearLayout) this.findViewById(R.id.weeklylist))
					.addView(rLayout);

		}

	}
	
	public void setonclick(WeeklyNotice weeklyNotice,RelativeLayout rlayout,final List<NoticeBean> notices,final int i){
		rlayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent =new Intent();
				intent.putExtra("title", notices.get(i).getTitle());
				intent.putExtra("content", notices.get(i).getMessage());
				intent.putExtra("writer", notices.get(i).getWriterid()+"");
				intent.setClass(WeeklyNotice.this, Showoneweekly.class);
				startActivity(intent);
			}
		});
	}

}
